<template>
  <section>
    <!--工具条-->
    <!-- <div>{{winWidth}}</div>
    <div>{{winHeight}}</div> -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input size="small" style="width:200px;" v-model.trim="filters.userName" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input size="small" style="width:200px;" v-model.trim="filters.url" placeholder="路径"></el-input>
        </el-form-item>
        <!--  <el-form-item>
          <el-input size="small" style="width:150px;" v-model.trim="filters.agrementNo" placeholder="业务关系代码"></el-input>
        </el-form-item>
        <el-form-item>
            <el-date-picker size="small" style="width:231px;" v-model="filters.Dates" type="daterange"  align="left"   placeholder="选择日期范围">
            </el-date-picker>
          </el-form-item> -->
        <el-form-item style="float:right;">
          <el-button size="small" type="primary" icon="search" v-show="serverinfoQuery" v-on:click="getLogs(true)">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="Logs" stripe border :height="winHeight-188" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" :cell-style="{height:'10px',color:'#ccc'}">
      <!-- <el-table-column type="selection" width="55">
      </el-table-column> -->
      <!-- <el-table-column prop="serialNo" label="序号" width="120" sortable>
      </el-table-column> -->
      <el-table-column prop="userName" label="名称" min-width="120">
        <!-- <el-tooltip class="item" effect="dark" :content="scope.row.shareId" placement="top"> -->
        <!-- <template scope="scope">
            <span style="height:20px;">{{scope.row.shareId}}</span>
          </template> -->
        <!-- </el-tooltip> -->
      </el-table-column>
      <el-table-column prop="url" label="路径" min-width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="connectTimeOut" label="超时时间" width="100">
      </el-table-column>
      <el-table-column prop="password" label="密码" min-width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="flag" label="标志" min-width="150" :formatter="formatFlag" sortable>
      </el-table-column>
      <el-table-column prop="description" label="描述" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="modifyDate" label="修改时间" min-width="180" sortable>
      </el-table-column>

      <el-table-column label="操作" fixed='right' min-width="90">
        <template scope="scope">
          <el-button size="small" type="warning" v-show="serverinfoUpdate" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->

      <!-- <el-pagination style="float:right"  layout="total" :total="total">
      </el-pagination> -->
      <el-pagination style="float:right" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[10, 20, 40, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-col>

    <!-- 修改 -->
    <el-dialog title="修改" v-model="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="路径" prop="url">
          <el-input v-model="editForm.url" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'

import commons from '../../common/js/common'
//import NProgress from 'nprogress'
import { mapState, mapActions } from 'vuex'
import { serviceInfo, updateServiceInfo } from '../../api/api';

export default {
  data() {
    return {
      filters: {
        userName: '',
        url: ''
      },
      editForm: {
        id: '',
        url: '',
        modifyDate: ''
      },
      Logs: [],
      lists: [],
      exportTotal: 0,
      total: 0,
      page: 1,
      pageSize: 10,
      loading: '',
      // fullscreenLoading:false,
      listLoading: false,
      editFormVisible: false,
      editLoading: false,
      serverinfoQuery: false,
      serverinfoUpdate: false,

      editFormRules: {
        url: [
          { required: true, message: '请输入路径', trigger: 'blur' }
        ],
      },
      sels: [],//列表选中列
      userid: '',
      IsShow: false
    }
  },
  computed: mapState([
    'winWidth',
    'winHeight'
  ]),
  methods: {
    cellStyle(row, rowIndex, columnIndex) {
    },
    formatStatus: function (row, column) {
      return row.level == '1' ? '一级' : (row.level == '2' ? '二级' : '三级');
    },
    formatTime: function (row, column) {
      return commons.getDaytime(row.modifyDate, 1, false);
    },
    formatFlag: function (row, column) {
      var str = '';
      switch (row.flag) {
        case '0':
          str = "注销";
          break;
        case '1':
          str = "记录日志";
          break;
        case '2':
          str = "不记录日志";
          break;
        case '3':
          str = "记录请求";
          break;
        case '4':
          str = "记录响应";
          break;
        case '5':
          str = "请求和响应均不记录";
          break;
      }
      return str;
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getLogs(true);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getLogs(true);
    },
    //获取用户列表
    getLogs(bool) {
      this.listLoading = bool;
      // this.fullscreenLoading = bool?false:true;
      if (!bool) {
        this.loading = this.$loading({
          lock: true,
          text: '正在导出',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      }
      let para = new FormData();
      para.append("pageSize", this.pageSize);
      para.append("pageNumber", this.page);
      para.append('userName', this.filters.userName);
      para.append('url', this.filters.url);
      serviceInfo(para).then((res) => {
        this.lists = res.data.result.content;
        this.total = res.data.result.totalElements;
        this.exportTotal = res.data.result.totalElements;
        this.listLoading = false;
        if (!bool) {
          this.loading.close();
          this.export2Excel();
        }
        else {
          this.Logs = res.data.result.content;
        }
      });
    },
    //table导出excel
    exportExcels: function () {
      this.getLogs(false);
    },
    export2Excel: function () {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../vendor/Export2Excel');
        const tHeader = ['分享标识', '分享类型', '产品ID', '业务关系代码', '销售人员', '销售人员编码', '创建用户', '点击次数', '创建时间'];
        const filterVal = ['shareId', 'shareType', 'productId', 'agrementNo', 'saleName', 'saleCode', 'userCode', 'sharinChain', 'createTime'];
        const list = this.lists;
        for (var i in list) {
          list[i].createTime = commons.getDaytime(list[i].createTime, 1, false);
        }
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '分享列表excel');
      })
    },
    formatJson: function (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      this.editForm.id = row.id;
      this.editForm.url = row.url;
      this.editForm.modifyDate = row.modifyDate;
    },
    //修改
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            let para = new FormData();
            para.append('id', this.editForm.id);
            para.append('url', this.editForm.url);
            para.append('modifyDate', this.editForm.modifyDate);
            updateServiceInfo(para).then((res) => {
              this.editLoading = false;
              if (res.data.success) {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.$refs['editForm'].resetFields();
                this.editFormVisible = false;
                this.getLogs(true);
              }
              else {
                this.$message({
                  message: res.data.result,
                  type: 'error'
                });
              }
            }).catch(() => {
              this.editLoading = false;
            });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    btnShow: function () {
      let thisData = JSON.parse(sessionStorage.getItem('161'));
      let _this = this;
      $.each(thisData, function (i, j) {
        if (thisData[i] == "serverinfoQuery") {
          _this.serverinfoQuery = true;
        }
        if (thisData[i] == "serverinfoUpdate") {
          _this.serverinfoUpdate = true;
        }
      });
    },
  },
  mounted() {
    this.btnShow();
    this.getLogs(true);
  }
}

</script>

<style scoped>

</style>